package com.alibaba.citrus.ots.common.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.jetbrains.annotations.NotNull;

/**
 * @author: qiaojiange
 * @create: 2021/10/15
 * @Description 错误信息工具
 */
public class ErrorInfoUtils {
    /**
     * 竖线
     */
    public static final String VERTICAL_LINE = "|";

    /**
     * 应用名称
     */
    public  static String appName = "cr-ots";

    /**
     * 错误信息
     */
    public static String errorMsg = "系统开小差了~";
    /**
     * 构建错误信息
     *
     *
     * @param argsBody 入参
     * @param point 切点
     * @param level 层次标识
     * @param cost 耗时
     * @return
     */
    public static String buildErrorInfo(String argsBody, ProceedingJoinPoint point, String level, long cost) {
        StringBuilder sb = new StringBuilder();
        sb.append(level).append(VERTICAL_LINE)
                //类名
                .append(AopUtils.getSimpleClassName(point)).append(VERTICAL_LINE)
                //方法名
                .append(AopUtils.getMethodName(point)).append(VERTICAL_LINE)
                //处理结果
                .append(false).append(VERTICAL_LINE)
                //耗时
                .append(cost).append(VERTICAL_LINE)
                //入参
                .append(argsBody).append(VERTICAL_LINE)
                //出参
                .append("").append(VERTICAL_LINE)
                //标识
                .append("").append(VERTICAL_LINE)
                //errorCode
                .append(appName).append(VERTICAL_LINE)
                .append(CommonErrorCode.SYSTEM_ERROR).append(VERTICAL_LINE)
                //默认错误信息
                .append(errorMsg);

        return sb.toString();
    }

    @NotNull
    public static String buildAccessLog(ProceedingJoinPoint point, long cost, AccessLogBody accessLogBody, String tag) {
        StringBuilder sb = new StringBuilder();
        sb.append(tag).append(ErrorInfoUtils.VERTICAL_LINE)
                //类名
                .append(AopUtils.getSimpleClassName(point)).append(ErrorInfoUtils.VERTICAL_LINE)
                //方法名
                .append(AopUtils.getMethodName(point)).append(ErrorInfoUtils.VERTICAL_LINE)
                //处理结果,不是默认结果说明调用成功
                .append(accessLogBody.getSuccess()).append(ErrorInfoUtils.VERTICAL_LINE)
                //耗时
                .append(cost).append(ErrorInfoUtils.VERTICAL_LINE)
                //入参
                .append(accessLogBody.getArgsBody()).append(ErrorInfoUtils.VERTICAL_LINE)
                //出参
                .append(accessLogBody.getResultBody()).append(ErrorInfoUtils.VERTICAL_LINE)
                //标识
                .append("").append(ErrorInfoUtils.VERTICAL_LINE)
                //errorCode
                .append(accessLogBody.getErrorCode() == null ? "" : accessLogBody.getErrorCode()).append(ErrorInfoUtils.VERTICAL_LINE)
                //默认错误信息
                .append(accessLogBody.getErrorMsg() == null ? "" : accessLogBody.getErrorMsg());
        return sb.toString();
    }
}
